package com.example.androidpattern.observer;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.androidpattern.AppConstant;
import com.example.androidpattern.EMTagHandler;
import com.example.androidpattern.R;
import com.example.androidpattern.observer.classes.ObserverA;
import com.example.androidpattern.observer.classes.ObserverB;
import com.example.androidpattern.observer.classes.Subject3D;
import com.example.androidpattern.observer.classes.SystemObserverA;
import com.example.androidpattern.observer.classes.SystemObserverB;
import com.example.androidpattern.observer.classes.SystemSubject2D;
import com.example.androidpattern.observer.classes.SystemSubject3D;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ObserverActivity extends AppCompatActivity {

    private static final String TAG = "ObserverActivity";
    @BindView(R.id.btn_observer_myself)
    Button btnObserverMyself;
    @BindView(R.id.btn_observer_system)
    Button btnObserverSystem;
    @BindView(R.id.tv_observer_info)
    TextView tvObserverInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_observer);
        ButterKnife.bind(this);

        initView();
    }

    private void initView() {
        setTitle("观察者模式");
        tvObserverInfo.setText(EMTagHandler.fromHtml(AppConstant.OBSERVER_DEFINE));

    }

    @OnClick({R.id.btn_observer_myself,
            R.id.btn_observer_system})
    public void onViewClicked(View view) {
        Log.d(TAG, "onViewClicked: ");
        switch (view.getId()) {
            case R.id.btn_observer_myself:
                observerByMyself();
                break;
            case R.id.btn_observer_system:
                observerBySystem();
                break;
        }
    }

    private void observerBySystem() {
        // 创建主题
        SystemSubject2D subject2D = new SystemSubject2D();
        SystemSubject3D subject3D = new SystemSubject3D();

        // 创建观察者
        SystemObserverA observerA = new SystemObserverA();
        SystemObserverB observerB = new SystemObserverB();

        // 订阅主题
        observerA.registerSubject(subject2D);
        observerA.registerSubject(subject3D);
        observerB.registerSubject(subject2D);

        // 主题分发消息
        subject2D.setMsg("2D 这是第一次分发的消息...");
        subject3D.setMsg("3D 这是第一次分发的消息...");
        Log.d(TAG, "observerBySystem: ===================");

        observerA.unregisterSubject(subject2D);
        subject2D.setMsg("2D 这是第二次分发的消息...");
        subject3D.setMsg("3D 这是第二次分发的消息...");
        Log.d(TAG, "observerBySystem: ===================");
    }

    private void observerByMyself() {
        // 创建主题
        Subject3D subject3D = new Subject3D();
        // 创建观察者A
        ObserverA observerA = new ObserverA();
        // 创建观察者B
        ObserverB observerB = new ObserverB();

        // 观察者订阅主题
        observerA.registerSubject(subject3D);
        observerB.registerSubject(subject3D);

        // 主题分发消息
        subject3D.setMsg("这是第一次分发的消息...");
        Log.d(TAG, "observerByMyself: ===================");

        // 观察者取消订阅主题
        observerA.unregisterSubject(subject3D);

        // 主题分发消息
        subject3D.setMsg("这是第二次分发消息...");
        Log.d(TAG, "observerByMyself: ===================");

    }
}